package com.ruoyi.web.controller.system.api.cc;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.Signatures;
import com.ruoyi.system.api.domain.StServiceReport;
import com.ruoyi.system.api.domain.dto.ServiceReportDto;
import com.ruoyi.system.api.domain.dto.ServiceReportMdQuestionDto;
import com.ruoyi.system.api.domain.dto.ServiceReportServiceFeeDto;
import com.ruoyi.system.api.domain.dto.ServiceReportTrainingInformationDto;
import com.ruoyi.system.api.service.CcService;
import com.ruoyi.system.api.service.IStServiceReportService;
import io.swagger.annotations.*;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 服务报告
 *
 * @author yzg
 * @ClassName ServiceReportController.java
 * @date 2020-03-02 13:48
 */
@Api(tags = "服务报告")
@RestController
@RequestMapping("/api/cc/service/report/")
public class ServiceReportController extends BaseController {

    @Autowired
    private IStServiceReportService stServiceReportService;

    @Autowired
    private CcService ccService;

    private static final Logger log = LoggerFactory.getLogger(ServiceReportController.class);

    /**
     * 编辑服务报告首页内容
     *
     * @param reportDto
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 2:20 下午
     */
    @Log(title = "编辑服务报告首页内容", businessType = BusinessType.UPDATE)
    @PostMapping("editServiceReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "编辑服务报告首页内容", httpMethod = "POST", notes = "编辑服务报告首页内容")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> editServiceReport(@RequestBody ServiceReportDto reportDto) {
        try {
            return stServiceReportService.editServiceReport(reportDto);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 编辑服务报告培训信息
     *
     * @param trainingInformationDto
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 1:13 下午
     */
    @Log(title = "编辑服务报告培训信息", businessType = BusinessType.UPDATE)
    @PostMapping("editServiceTrainingInformation")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "编辑服务报告培训信息", httpMethod = "POST", notes = "编辑服务报告培训信息")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> editServiceTrainingInformation(@RequestBody ServiceReportTrainingInformationDto trainingInformationDto) {
        try {
            return stServiceReportService.editServiceTrainingInformation(trainingInformationDto);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 编辑服务报告服务费用
     *
     * @param serviceFeeDto
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 1:39 下午
     */
    @Log(title = "编辑服务报告服务费用", businessType = BusinessType.UPDATE)
    @PostMapping("editServiceFee")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "编辑服务报告服务费用", httpMethod = "POST", notes = "编辑服务报告服务费用")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> editServiceFee(@RequestBody ServiceReportServiceFeeDto serviceFeeDto) {
        try {
            return stServiceReportService.editServiceFee(serviceFeeDto);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 编辑服务报告MD问题
     *
     * @param mdQuestionDto
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 11:16 上午
     */
    @Log(title = "编辑服务报告MD问题", businessType = BusinessType.UPDATE)
    @PostMapping("editMdQuestion")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "编辑服务报告MD问题", httpMethod = "POST", notes = "编辑服务报告MD问题")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> editMdQuestion(@RequestBody ServiceReportMdQuestionDto mdQuestionDto) {
        try {
            return stServiceReportService.editMdQuestion(mdQuestionDto);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查看服务报告详情
     *
     * @param id
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 11:03 上午
     */
    @GetMapping("detailServiceReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看服务报告详情", httpMethod = "GET", notes = "查看服务报告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "FSE enginnerId", name = "enginnerId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "服务报告主键ID", name = "reportId", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> detailServiceReport(
            @RequestParam(value = "serviceNo") String serviceNo,
            @RequestParam(value = "enginnerId") Long enginnerId,
            @RequestParam(value = "reportId", required = false) Long reportId) {
        try {
            return stServiceReportService.detailServiceReport(serviceNo, enginnerId, reportId);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查看服务报告费用详情
     *
     * @param
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 11:03 上午
     */
    @GetMapping("detailServiceReportFee")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看服务报告费用详情", httpMethod = "GET", notes = "查看服务报告费用详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "服务报告辅助id", name = "helpId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师ID", name = "engineerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "BU", name = "bu", required = true, defaultValue = "ICLC", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "省份", name = "prov", required = true, defaultValue = "吉林省", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "chargeHours", name = "chargeHours", required = true, defaultValue = "长春市", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "城市", name = "city", required = true, defaultValue = "长春市", dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> detailServiceReportFee(
            @RequestParam(value = "serviceNo") String serviceNo,
            @RequestParam(value = "helpId", required = false) String helpId,
            @RequestParam(value = "engineerId") Long engineerId,
            @RequestParam(value = "bu", required = true) String bu,
            @RequestParam(value = "prov", required = true) String prov,
            @RequestParam(value = "chargeHours", required = true) Double chargeHours,
            @RequestParam(value = "city", required = true) String city
    ) {
        try {
           // return stServiceReportService.detailServiceReportFee(serviceNo, engineerId, helpId, bu, prov, city);
            return stServiceReportService.detailServiceReportFee2(serviceNo, engineerId, helpId, bu, prov, city,chargeHours);

        } catch (Exception e) {
            e.printStackTrace();

            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 提交服务报告
     *
     * @param id         服务报告主键ID
     * @param enginnerId 提交的工程师ID
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/10 2:49 下午
     */
    @RepeatSubmit
    @Log(title = "提交服务报告", businessType = BusinessType.UPDATE)
    @GetMapping("submitServiceReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "提交服务报告", httpMethod = "GET", notes = "提交服务报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务报告主键ID", name = "id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "提交的工程师ID", name = "enginnerId", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> submitServiceReport(@RequestParam("id") Long id, @RequestParam("enginnerId") Long enginnerId) {
        try {
            return stServiceReportService.submitServiceReport(id, enginnerId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 2.25验收签名照片、发送 邮件（服务报告）
     *
     * @methodName: getServiceRportSignature
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: sjy
     * @date: 2020/3/3 15:23
     **/
    @Log(title = "2.25验收签名照片、发送 邮件（服务报告）", businessType = BusinessType.UPDATE)
    @ResponseBody
    @RequestMapping(value = "/getServiceRportSignature", method = RequestMethod.POST)
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "2.25验收签名照片、发送 邮件（服务报告）", httpMethod = "POST", notes = "2.25验收签名照片、发送 邮件（服务报告）")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getServiceRportSignature(@RequestBody Signatures signatures) {
        try {
            return stServiceReportService.getServiceRportSignature(signatures);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 查询日志列表
     *
     * @param serviceNo
     * @param enginnerId
     * @methodName: getReportList
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: sjy
     * @date: 2020年3月19日08:50:55
     **/
    @GetMapping("/getReportList")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取日志列表", httpMethod = "GET", notes = "获取日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "001", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", required = true, defaultValue = "001", dataType = "Long", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功", response = StServiceReport.class)
    })
    public ResponseWithData<?> getReportList(@RequestParam(value = "serviceNo", required = true) String serviceNo,
                                             @RequestParam(value = "enginnerId", required = true) Long enginnerId) {
        try {
            StServiceReport stServiceReport = new StServiceReport();
            stServiceReport.setServiceNo(serviceNo);
            stServiceReport.setEnginnerId(enginnerId);
            List<StServiceReport> list = stServiceReportService.selectStServiceReportList(stServiceReport);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 4服务费接口
     *
     * @param engineerId
     * @param bu
     * @param prov
     * @param city
     * @methodName: getServiceCharge
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/24 14:01
     **/
    @GetMapping("getServiceCharge")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "4服务费接口", httpMethod = "GET", notes = "4服务费接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师ID", name = "engineerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "BU", name = "bu", required = true, defaultValue = "ICLC", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "省份", name = "prov", required = true, defaultValue = "吉林省", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "城市", name = "city", required = true, defaultValue = "长春市", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getServiceCharge(
            @RequestParam(value = "engineerId", required = true) String engineerId,
            @RequestParam(value = "bu", required = true) String bu,
            @RequestParam(value = "prov", required = true) String prov,
            @RequestParam(value = "city", required = true) String city
    ) {
        try {
            JSONObject response = ccService.GetServiceCharge(engineerId, bu, prov, city);
            return new ResponseWithData<>().success(response).msg("操作成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


}
